/************************************************************************
Copyright 2003, 2004, 2005, 2006, 2007, 2008 Grid Technology Research Center,
National Institute of Advanced Industrial Science and Technology.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
************************************************************************/

package jp.aist.gtrc.plus.scheduler.specific.pbs.status;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.log4j.Logger;

public class StatusMapImpl implements Iterable<Map.Entry<String, StatusValue>>, StatusMap {

	private final Map<String, StatusValue> map;
	private final String name;

	protected static final Logger logger = Logger.getLogger(StatusMapImpl.class);

	StatusMapImpl(String name){

		this.map = new LinkedHashMap<String, StatusValue>();
		this.name = name;

	}

	public String getName(){

		return name;

	}

	public void putStatus(String attr, String value){

		map.put(attr, new StatusValue(value));

	}

	public void putStatusMap(String name, StatusMap status){

		map.put(name, new StatusValue(status));

	}

	public String getStatus(String attr){

		StatusValue value = map.get(attr);
		if (value != null) {
			return value.getStatus();
		} else {
			logger.debug("unknown/unspecified status: " + attr + " for " + getName());
			return UNKNOWN_STATUS;
		}

	}

	public StatusMap getStatusMap(String name){

		StatusValue value = map.get(name);
		if (value != null) {
			return value.getChildMap();
		} else {
			// allocate new map is not exists
			logger.debug("allocate new StatusMap: " + name);
			StatusMapImpl child = new StatusMapImpl(name);
			map.put(name, new StatusValue(child));
			return child;
		}

	}

	public String getStatus(String name, String attr){

		StatusValue value = map.get(name);
		if (value != null) {
			StatusMap child = value.getChildMap();
			if (child != null) {
				return child.getStatus(attr);
			}
		}

		logger.debug("unknown status: " + name + "@" + attr + " for " + getName());
		return UNKNOWN_STATUS;

	}

	public Iterator<Map.Entry<String, StatusValue>> iterator(){

		return map.entrySet().iterator();

	}

	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append(getName());
		sb.append("\n");

		for (Map.Entry<String, StatusValue> e : this) {
			sb.append("\t");
			sb.append(e.getKey());
			sb.append("\t");
			sb.append(e.getValue().toString());
			sb.append("\n");
		}

		return sb.toString();
	}

}
